/*
 * @Description: vue配置文件
 * @Author: zhangxu
 * @Date: 2022-01-24 17:15:13
 */
const path = require("path");
const CompressionPlugin = require("compression-webpack-plugin");
console.log("当前环境：" + process.env.VUE_APP_ENV);
function resolve(dir) {
  return path.join(__dirname, dir);
}
module.exports = {
  transpileDependencies: ["*"], //所有依赖经过babel-loader处理
  // publicPath: "/digitalizeStore/knowledge/p1/", //部署应用包时的基本 URL联通测试环境使用  代理后的正式环境
  publicPath: "/manager/", //亦庄用
  // 将构建好的文件输出到哪里
  outputDir: "dist",
  // 放置生成的静态资源(js、css、img、fonts)的目录。
  assetsDir: "static",
  // 指定生成的 index.html 的输出路径
  indexPath: "index.html",
  //如果不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  // 配置css
  css: {
    sourceMap: true,
    // css预设器配置项
    loaderOptions: {
      less: {
        // 解决Vue antdv 报错 .bezierEasingMixin()
        javascriptEnabled: true,
        modifyVars: {
          // 直接覆盖变量
          "primary-color": "#1067ee",
        },
      },
    },
    requireModuleExtension: true,
  },
  chainWebpack: (config) => {
    if (process.env.VUE_APP_ENV === "analyzer") {
      config
        .plugin("webpack-bundle-analyzer")
        .use(require("webpack-bundle-analyzer").BundleAnalyzerPlugin)
        .end();
      config.plugins.delete("prefetch");
    }
    // 添加别名
    config.resolve.alias
      .set("@", resolve("src"))
      .set("@/assets", resolve("src/assets"))
      .set("@/plugins", resolve("src/plugins"))
      .set("@/components", resolve("src/components"))
      .set("@/views", resolve("src/views"))
      .set("@/services", resolve("src/services"))
      .set("@/utils", resolve("src/utils"))
      .set("@/mock", resolve("src/mock"));
    config.optimization.minimizer("terser").tap((args) => {
      // 去除生产环境console
      args[0].terserOptions.compress.drop_console = true;
      return args;
    });
  },
  configureWebpack: (config) => {
    const plugins = [];
    // gzip压缩
    if (process.env.NODE_ENV === "production") {
      config["performance"] = {
        //打包文件大小配置
        maxEntrypointSize: 10000000,
        maxAssetSize: 30000000,
      };
      plugins.push(
        new CompressionPlugin({
          // gzip压缩配置
          test: /\.js$|\.html$|.\css/, // 匹配文件名
          threshold: 10240, // 对超过10k的数据压缩
          deleteOriginalAssets: false, // 不删除源文件
        })
      );
    }
    config.plugins = [...config.plugins, ...plugins];
  },
  devServer: {
    open: true, //是否自动弹出浏览器页面
    https: false, // https:{type:Boolean}
    disableHostCheck: true, //内网穿透使用的--自用
    port: 8082,
    hot: true,
    overlay: {
      warning: false,
      error: false,
    },
    proxy: {
      //跨域代理
      "/manager/api": {
        // target: "http://10.162.145.27:8501/", //dev开发
        // target: "http://10.242.31.70:8092/", //目标代理服务器地址
        target: "http://10.162.145.27:8131/", //测试
        changeOrigin: true, //允许跨域
        secure: false, //设置证书免校验
        ws: false, // 代理的WebSockets
        pathRewrite: {
          "^/manager/api": "",
        },
      },
      "/manager/apiClear": {
        // target: "http://192.168.133.31:8131/", //目标代理服务器地址
        target: "http://10.242.31.70:8082/", //目标代理服务器地址
        changeOrigin: true, //允许跨域
        secure: false, //设置证书免校验
        ws: false, // 代理的WebSockets
        pathRewrite: {
          "^/manager/apiClear": "",
        },
      },
      "/manager/push": {
        target: "http://10.162.145.28:8124/", //目标代理服务器地址
        changeOrigin: true, //允许跨域
        secure: false, //设置证书免校验
        ws: false, // 代理的WebSockets
        pathRewrite: {
          "^/manager/push": "",
        },
      },
    },
  },
};
